package impl

import (
	"context"

	"gitee.com/go-course/go12/vblog/apps/comment"
	"gitee.com/go-course/go12/vblog/common"
	"gitee.com/go-course/go12/vblog/conf"
	"gitee.com/go-course/go12/vblog/ioc"
	"google.golang.org/grpc"
	"gorm.io/gorm"
)

func init() {
	ioc.Controller().Registry(&CommentServiceImpl{})
}

// 需要实现完整的接口, 通过也要实现grpc对外接口
type CommentServiceImpl struct {
	// db
	db *gorm.DB

	// rpc 能力继承
	comment.UnimplementedRPCServer
}

// 对外暴露 grpc能力 通过gprc server
func (h *CommentServiceImpl) Registry(r *grpc.Server) {
	comment.RegisterRPCServer(r, h)
}

func (i *CommentServiceImpl) Init() error {
	// db ioc
	i.db = conf.C().MySQL.GetConn().Debug()
	return nil
}

func (i *CommentServiceImpl) Name() string {
	return comment.AppName
}

// RPC接口
func (i *CommentServiceImpl) CreateComment(
	ctx context.Context, in *comment.CreateCommentRequest) (
	*comment.Comment, error) {
	ins := &comment.Comment{
		Meta: common.NewMeta(),
		Spec: in,
	}
	return ins, nil
}
